#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

void setup() {
  Serial.begin(9600);              // Initialize serial communications with the PC
  SPI.begin();                     // Init SPI bus
  mfrc522.PCD_Init();              // Init MFRC522 card
  Serial.println("read ID card ......");
}

void loop() {
  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  //some variables we need
  byte block;
  byte len;
  MFRC522::StatusCode status;
  // 如果传感器/读卡器上没有新卡，则重置回路。这在空闲时节省了整个进程。
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  // 选择一张卡片
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  block = 4;
  len = 18;
  byte buffer2[18];
  block = 1;
  
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  status = mfrc522.MIFARE_Read(block, buffer2, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  //打印信息
  for (uint8_t i = 0; i < 16; i++) {
    Serial.write(buffer2[i]);
  }
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  Serial.println("");
}
